package com.yanqu.road.server.timer.player;

import com.yanqu.road.entity.player.PlayerState;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.musicroom.MusicRoomModule;
import com.yanqu.road.server.gameplayer.module.player.EarnSpeedModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 重置在线玩家每日数据
 */
public class PlayerDataHourResetTask extends GeneralTask {

	private static Logger logger = LogManager.getLogger(PlayerDataHourResetTask.class.getName());
	private static long lastCheckHour = -1;

	@Override
	public void doRun() {
		boolean hourReset = false;
		int nowHours = DateHelper.getHours();
		if (nowHours != lastCheckHour) {
			lastCheckHour = nowHours;
			hourReset = true;
		}

		if (hourReset) {
			for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
				if (player.getState() == PlayerState.ONLINE) {
					hourPass(player);
				}
			}
		}

	}

	private void hourPass(GamePlayer player) {
		// 每小时检查一次
		try {
			player.getModule(MusicRoomModule.class).resetOneDayByHour();
		} catch (Exception e) {
			logger.error("每小时检查异常 {}", e);
		}
	}
}
